﻿
namespace Agility.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using Agility.Web.Services.RepositoryDomainServicePattern;
    using Agility.Web.Repositories;
    using Agility.Web.Models.DomainModel;
using NLog;

    [EnableClientAccess()]
    [RequiresAuthentication]
    public class RequirementsManagementService : RepositoryDomainService
    {
        private IEpicRepository epicRepository;

        private static Logger logger = LogManager.GetCurrentClassLogger();

        public RequirementsManagementService(IEpicRepository epicRepository) : base(epicRepository)
        {
            logger.Debug("--> Entering Constructor");
            if (epicRepository == null)
            {
                ArgumentNullException argumentNullException = new ArgumentNullException("epicRepository");
                logger.ErrorException(argumentNullException.Message, argumentNullException);
                logger.Warn("Tried to create an instance of RequirementsMgmtService without a repository");
                throw argumentNullException;
            }
            this.epicRepository = epicRepository;
            logger.Debug("<-- Leaving Constructor");
        }

        [Query]
        public IQueryable<Epic> GetMyIdeas()
        {
            logger.Debug("--> Entering GetMyIdeas");
            string username = this.ServiceContext.User.Identity.Name;
            logger.Trace("Querying epics for user {0}", username);
            IQueryable<Epic> myIdeas = this.epicRepository.GetEntities().Where(e => e.RequestedBy == username);
            logger.Debug("<-- Leaving GetMyIdeas returning {0} epics", myIdeas.Count());
            if (logger.IsTraceEnabled)
                foreach(Epic epic in myIdeas)
                    logger.Trace<Epic>(epic);
            return myIdeas;
        }

        [Insert]
        public void InsertEpic(Epic epic)
        {
            logger.Debug("--> Entering InsertEpic");
            this.InsertEntity(epic);
            logger.Debug("Inserted Epic");
            logger.Trace<Epic>(epic);
            logger.Debug("<-- Leaving InsertEpic");
        }

        public void UpdateEpic(Epic updated)
        {
            logger.Debug("--> Entering UpdateEpic");
            this.UpdateEntity(updated);
            logger.Debug("Updated Epic");
            logger.Trace<Epic>("Updated Epic", updated);
            logger.Debug("<-- Leaving UpdateEpic");
        }

        //[Query]
        //public IQueryable<Epic> GetEpics()
        //{
        //    return this.epicRepository.GetEntities();
        //}

        //public IQueryable<Epic> GetEstimatedEpics()
        //{
        //    return this.epicRepository.GetEpics().Where(e => e.RankingValue.HasValue).OrderByDescending(e => e.RankingValue);
        //}
    }
}


